<?php
function findline($needle, $datafile)
{
	$line = 0;
	$lines = count($datafile);
	
	for ($l = 0; $l < $lines; $l++)
	{
		$line=-1;
		$strlength = strlen ($needle);
		$checkline = substr($datafile[$l],0,$strlength);
		if ($checkline == $needle) {$line = $l; break;}
	}
	
	return $line;
}

function read_files($files)
{
	$path = $files["path"];
	$location = $files["location"];
	$count = $files["count"];


	// get data from $count files 
	for ($i = 0; $i < $count; $i++)
	{
		// open datafile
		$datafile = file($path[$i]);
		$line = 0;
		// get date from line 1
		$year = substr($datafile[1],30,4);
		$month = substr($datafile[1],35,2);
		$day = substr($datafile[1],38,2);
		$hour = substr($datafile[1],41,2);
		$minute = substr($datafile[1],43,2);

		// make timestamp
		$mktime = mktime ($hour,$minute,0,$month,$day,$year); 
		$timestamp = date("U",$mktime);

		// get temperature
		$line  = findline("Temperature", $datafile);
		if($line > 0) {
			$tempF = substr($datafile[$line],13,3); 
			$temp = ($tempF - 32) * (5/9);
			$temp = round($temp,1);
			if ($temp == "-17.8") {$timestamp = 0;}
		}
		else {
			$temp = "";
		}

		// get sky condition
		$line = findline("Sky conditions", $datafile);
		if($line > 0) {
			$sky = substr($datafile[$line],16,20); 
			switch ($sky) {
				case "clear\n":   $sky = 'helder'; $n='0'; break;
				case "mostly clear\n": $sky = 'licht bewolkt'; $n='1-2'; break;
				case "partly cloudy\n": $sky = 'half bewolkt'; $n='3-4'; break;
				case "mostly cloudy\n": $sky = 'bewolkt'; $n='5-7'; break;
				case "overcast\n":   $sky = 'bewolkt'; $n='8'; break;
			}
		}
		else {
			$sky = "";
		}
		
		
		// get pressure
		$line  = findline("Pressure", $datafile);
		if($line > 0) {
			$pressure = substr($datafile[$line],36,4);
			$pressure = $pressure + 0;
			if ($pressure == "") {$timestamp = 0;}
		}
		else {
			$pressure = "";
		}

		// get humidity
		$line = findline("Relative Humidity", $datafile);
		if($line > 0) {
			$humidity = substr($datafile[$line],19,4);
			$humidity = str_replace ("%", " ", $humidity); 
			$humidity = chop($humidity);
		}	
		else {
			$humidity = "";
		}

		// get wind
		$line = findline("ob", $datafile);
		if($line > 0) {
			$winddeg = substr($datafile[$line],17,3);
			$KN = substr($datafile[$line],20,2);
			$windspeed = $KN * 1.85;
			$windspeed = round($windspeed); 
			switch (true) {
		    	case ($winddeg >= 337):   $winddir = 'N'; break;
		     	case ($winddeg < 22):   $winddir = 'N'; break;
		     	case ($winddeg >= 22 AND $winddeg < 67):   $winddir = 'NO'; break;
		     	case ($winddeg >= 67 AND $winddeg < 112):   $winddir = 'O'; break;
		     	case ($winddeg >= 112 AND $winddeg < 157):   $winddir = 'ZO'; break;
		     	case ($winddeg >= 157 AND $winddeg < 202):   $winddir = 'Z'; break;
		     	case ($winddeg > 202 AND $winddeg < 247):   $winddir = 'ZW'; break;
		     	case ($winddeg >= 247 AND $winddeg < 292):   $winddir = 'W'; break;
		     	case ($winddeg >= 292 AND $winddeg < 337):   $winddir = 'NW'; break;
	      	}
		}
		else {
			$winddir = "";
			$windspeed = "";
		}

		//obtain data;
		$weatherdata[$location[$i]]["timestamp"] = $timestamp;
		$weatherdata[$location[$i]]["temp"] = $temp;
		$weatherdata[$location[$i]]["sky"] = $sky;
		$weatherdata[$location[$i]]["pressure"] = $pressure;
		$weatherdata[$location[$i]]["humidity"] = $humidity;
		$weatherdata[$location[$i]]["windir"] = $winddir;
		$weatherdata[$location[$i]]["windspeed"] = $windspeed;
  	}

	return $weatherdata;
}

?>